//
// Created by jerry on 2024/5/18.
//
#include "binaryTree.h"
#define TElemtype BinaryTreeNode *
#include "stack.h"
void PreOrderTraverse(BiTree tree, int reverse)
{
#ifndef RECURSIVE
    SqStack stack;
    InitStack(&stack);
    BiTNode *p = tree;
    while (p || !StackEmpty(stack))
    {
        while (p)
        {
            printf("%c\t", p->data);
            Push(&stack, p);
            if (reverse == 0)
                p = p->left;
            else
                p = p->right;
        }
        if (!StackEmpty(stack))
        {
            Pop(&stack, &p);
            if (reverse == 0)
                p = p->right;
            else
                p = p->left;
        }
    }
    DestroyStack(&stack);
#else
    if (reverse == 0)
    {
        printf("%c\t", tree->data);
        if (tree->left)
            PreOrderTraverse(tree->left, reverse);
        if (tree->right)
            PreOrderTraverse(tree->right, reverse);
    }
    else
    {
        if (tree->right)
            PreOrderTraverse(tree->right, reverse);
        if (tree->left)
            PreOrderTraverse(tree->left, reverse);
        printf("%c\t", tree->data);
    }

#endif
}
#ifdef Elemtype
#undef Elemtype
#define Elemtype int
#endif

BiTNode *initBinaryTreeNode(Elemtype data)
{
    BiTNode *node = NULL;
    MALLOC(node, BiTNode, 1);
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return node;
}
Status DestroyBiT(BiTree tree)
{
    if (tree->left)
        DestroyBiT(tree->left);
    if (tree->right)
        DestroyBiT(tree->right);
    free(tree);
    return 1;
}
